require("Key")

StageParser = class("StageParser")

---

function StageParser:initialize(stage)

	self.stage = stage
	
	self:parseRooms()
	self:parseDoors()
	self:createKeys()
	
	--debug.printTable(gGame.reality, 5)	
end

---

function StageParser:parseRooms()

	for roomName, roomDescriptor in pairs(self.stage.Rooms) do
		
		local room = gField.world:addLocation(roomName, "Room")
		
		
		-- add furnitures
		
		if roomDescriptor.Furnitures then
			for furnitureName, furnitureDescriptor in pairs(roomDescriptor.Furnitures) do
			
				room:addLocation(furnitureName, "Furniture")
			end
		end
	end
end

---

function StageParser:parseDoors()

	for i, doorDescriptor in ipairs(self.stage.Doors) do

		self:addDoor(doorDescriptor.Rooms[1], doorDescriptor.Rooms[2], doorDescriptor.ClosedAndLocked)
	end	
end

---

function StageParser:addDoor(location1Name, location2Name, closedAndLocked)

	local location1 = gField.entityTypes.Location[location1Name]
	local location2 = gField.entityTypes.Location[location2Name]
	
	if location1.subContainers[location2.name] then
	
		location1:addDoorToContainedLocation(
			location2, 
			doorDescriptor.ClosedAndLocked
		)
	else
		gField.world:addDoorBetweenContainedLocations(
			location1, 
			location2, 
			closedAndLocked
		)
	end
end

---

function StageParser:createKeys()

	for doorName, doorEntity in pairs(gField.entityTypes.Door) do
		
		if not doorEntity:isEnabled() then
		
			Key:new(doorName)
		end
	end
end